package com.jd.dl.web;

import java.util.ArrayList;
import java.util.List;

import com.jd.dl.pojo.Dictionary;
import com.jd.dl.pojo.Task;
import com.jd.dl.pojo.User;
import com.jd.dl.service.DictionaryService;
import com.jd.dl.service.TaskService;
import com.jd.dl.service.UserService;
import com.jd.dl.service.exception.ServiceException;
import com.jd.dl.util.Pager;
import com.jd.dl.web.filter.TaskFilter;



public class TaskAction extends BaseAction {
	private static final long serialVersionUID = -4018571604337249963L;
	private static final long DEFAULT_TASK_STATUS_ID = 101;
	private UserService userService;
	private TaskService taskService;
	private DictionaryService dictionaryService;
	private TaskFilter filter;
	private List<User> users = new ArrayList<User>();
	private String values;
	
	private Task task = new Task();
	private Long taskId;
	

	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	
	public String taskInfo(){
		if(taskId != null){
			task = taskService.get(taskId);
		}
		loadResource();
		return SUCCESS;
	}
	
	public String taskAdd(){
		loadResource();
		return SUCCESS;
	}
	
	private void loadResource(){
		users = userService.getList(null);
	}
	
	public String taskList(){
		pager = getPagerObject();
		List<Task> res = (List<Task>) taskService.getList(pager, getFilter().toHqlString(),
				getSortStr());
		pager.setDataRows(res);
		return SUCCESS;
	}
	
	public TaskFilter getFilter() {
		if (filter == null) {
			filter = new TaskFilter();
		}

		return filter;
	}
	
	public List<User> getUsers(){
		return users;
	}
	
	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String taskUpdate() throws ServiceException {
		try {
			if (task.getTaskId() == null) {
				Dictionary defaultTaskStatus = dictionaryService.load(DEFAULT_TASK_STATUS_ID);
				task.setTaskStatus(defaultTaskStatus);
				taskService.add(task);
			} else {
				taskService.update(task);
			}
		} catch (ServiceException e) {
			addActionError(e.getErrorMessage());
			return INPUT;
		}
		return SUCCESS;
	}

	public Pager getPager() {
		return pager;
	}
	
	public String delete(){
		try {
			if(values != null){
				String[] ids = values.split(",");
				for(String id: ids){
					taskService.delete(Long.valueOf(id));
				}
			}else{
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String search(){
		pager = getPagerObject();
		List<Task> res = (List<Task>) taskService.getList(pager, getFilter().toHqlString(),
				getSortStr());
		pager.setDataRows(res);
		return SUCCESS;
	}

	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	public Long getTaskId() {
		return taskId;
	}
	public void setTaskId(Long taskId) {
		this.taskId = taskId;
	}
	public String getValues() {
		return values;
	}
	public void setValues(String values) {
		this.values = values;
	}
	public DictionaryService getDictionaryService() {
		return dictionaryService;
	}
	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

}
